<?php

namespace app\common\controller;

use think\cache\driver\Redis;
use think\Controller;
/**
 * 公共资源-实名信息 验证
 */
class IdcardAuth extends Controller
{
    public $appId = 'hYKF56TR';
    public $appKey = 'd0OiQL1c';
    //public $url = 'https://api.253.com/open/idcard/id-card-auth/vs';
    public $url = 'https://api.253.com/open/idcard/id-card-auth';
    public $redis = '';
    public function initialize(){
        $this->redis = new Redis(getRedisConfig());
    }
    /**
     * 实名信息 验证
     * @param str $idcard 身份号
     * @param str $username 姓名
     * @return 数据信息| 1 成功 0失败
     * @since 2023/08/14
     * @author ZhangXuanNeng
     */
    public function idcardAuthVs($idcard,$username){
        $key = 'cqyqsdk_'.$idcard;
        $ageStatus = $this->redis->get($key);
        if($ageStatus){
            if($ageStatus != $username){
                return ['code'=>-1,'msg'=>'认证不一致'];
            }
            $age = $this->Getpartmessage($idcard);
            $data = array(
                'age'   => $age['age'],
                'age_status'=> $age['age'] >= 18 ? 1 : 2,
                'name'  => $username
            );
            return ['code'=>0,'msg'=>'认证一致','data'=>$data];
        }else{
            $postArr = array(
                'appId'  => $this->appId,
                'appKey' => $this->appKey,
                'name'   => $username,
                'idNum'  => $idcard,
            );
//            $str = "appId".$this->appId.'idNum'.$idcard.'name'.$username;
//            $postArr['sign'] = base64_encode(hash_hmac('sha1',$str,$this->appKey,true));
            $result = $this->Curl($postArr);
            if (isset($result['code'])  && $result['code'] == '200000'){
                if($result['data']['result'] == '01'){
                    $data = array(
                        'age'   => $result['data']['age'],
                        'age_status'=> $result['data']['age'] >= 18 ? 1 : 2,
                        'name'  => $username
                    );
                    $this->redis->set($key,$username,86400 * 365);
                    return ['code'=>0,'msg'=>'认证一致','data'=>$data];
                }else{
                    return ['code'=>-1,'msg'=>'认证不一致'];
                }

            } else {
                return ['code'=>-1,'msg'=>$result['message']];
            }
        }

    }
    public function Curl($postFields){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->url);
        curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        $ret = curl_exec($curl);
        curl_close($curl);
        $result = json_decode($ret, TRUE);
        return $result;
    }

    public function Getpartmessage($identify){
        //获取性别
        $sexint = (int)substr($identify, 16, 1);
        if ($sexint % 2 === 0) {
            $data['sex'] = '女';
        } else {
            $data['sex'] = '男';
        }
        //获取生日
        $bir = substr($identify, 6, 8);
        $year = (int)substr($bir, 0, 4);
        $month = (int)substr($bir, 4, 2);
        $day = (int)substr($bir, 6, 2);
        $data['birthday'] = $year . "-" . $month . "-" . $day;
        //获取年龄
        $date2 = strtotime(substr($identify, 6, 8));
        #  获得今日的时间戳
        $today = strtotime('today');
        #  得到两个日期相差的大体年数
        $diff = floor(($today - $date2) / 86400 / 365);
        #  strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
        $data['age'] = strtotime(substr($identify, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
        return $data;
    }
}
